import java.util.*;

class User //登入者类
{
    public String username;//登录账号
    public String password;//登录密码

    User(String username, String password) 
    {
        this.username = username;
        this.password = password;
    }
}

class Singer //歌手类
{
    public String name;//歌手名
    public double[] score;//所得评分数组
    public double _sum;//最终评分
    public int _size;//评分次数

    Singer(String name) 
    {
        this.name = name;
        score = new double[50];
        _sum=0.0;
        _size=0;
    }

    public double[] Sort()
    {
        Arrays.sort(score);
        int left=0;
        int right=49;
        while(left<right)
        {
            double k=score[left];
            score[left]=score[right];
            score[right]=k;
            left++;
            right--;
        }
        return score;
    }

    public double Sum(int x,int y)
    {
        _sum=0;
        for(int k=x;k<y;k++)
        {
            _sum+=score[k];
        }
        return _sum;
    }
}

public class SingerCompetitionSystem 
{
    public static List<User> users = new ArrayList<>();
    public  static List<Singer> singers = new ArrayList<>();
    public  static Scanner scanner = new Scanner(System.in);
    public  static User currentUser = null;

    public static void main(String[] args) 
    {
        users.add(new User("admin", "admin"));
        while (true) //登录界面
        {
            System.out.print("\033[2J");
            System.out.println("**************************************");
            System.out.println("**************************************");
            System.out.println("****** 欢迎来到歌手评选打分系统 ******");
            System.out.println("**************************************");
            System.out.println("******  1.注册  2.登录  3.退出  ******");
            System.out.println("**************************************");
            System.out.println("**************************************");
            System.out.print("请输入你的选项:");
            int choice = scanner.nextInt();
            scanner.nextLine(); 

            switch (choice) //选择分类
            {
                case 1:
                    register();
                    break;
                case 2:
                    login();
                    break;
                case 3:
                    System.exit(0);
                default:
                    System.out.println("选项错误请重新选择！！！！！！");
            }
            if (currentUser != null) {
                showMenu();
            }
            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void register() //注册模块
    {
        System.out.print("请输入注册的用户名: ");
        String username = scanner.nextLine();
        while(true)
        {
            System.out.print("请输入密码: ");
            String password1 = scanner.nextLine();
            System.out.print("请输入确认密码: ");
            String password2 = scanner.nextLine();
            if(password1.equals(password2))
            {
                users.add(new User(username, password1));
                break;
            }
            try
            {
                Thread.sleep(1000);
                System.out.println("两次密码不一样，请重新输入!!!");
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        System.out.println("注册成功!");
        try
        {
            Thread.sleep(1000);
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    public static void login() //登录模块
    {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();
            for (User user : users) 
            {
                if (user.username.equals(username) && user.password.equals(password)) 
                {
                    try
                    {
                        currentUser = user;
                        System.out.println("登录成功!");
                        Thread.sleep(1000);
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    System.out.print("\033[2J");
                    return;
                }
            }
        System.out.println("用户名或密码错误!");
    }

    public static void showMenu() 
    {
        while (true) {
            System.out.print("\033[2J");
            System.out.println("********************************************");
            System.out.println("********************************************");
            System.out.println("*****  1.录入歌手信息    2.给歌手打分  *****");
            System.out.println("********************************************");
            System.out.println("***** 3.查询数据 4.成绩排序 5.退出登录 *****");
            System.out.println("********************************************");
            System.out.println("********************************************");
            System.out.print("请输入你的选项:");
            int choice = scanner.nextInt();
            scanner.nextLine(); // consume newline
            switch (choice) 
            {
                case 1:
                    addSinger();
                    break;
                case 2:
                    rateSinger();
                    break;
                case 3:
                    queryData();
                    break;
                case 4:
                    sortScores();
                    break;
                case 5:
                    currentUser = null;
                    return;
                default:
                    System.out.println("选项错误请重新选择！！！！！！");
            }
            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void addSinger() //歌手录入模块
    {
        System.out.print("请输入歌手姓名: ");
        String name = scanner.nextLine();
        singers.add(new Singer(name));
        System.out.println("歌手信息录入成功!");
    }

    public static void rateSinger() //歌手评分模块
    {
        System.out.print("请输入歌手姓名: ");
        String name = scanner.nextLine();
        for (Singer singer : singers) 
        {
            if (singer.name.equals(name)) 
            {
                System.out.print("请输入分数: ");
                double score = scanner.nextDouble();
                singer.score[singer._size++] = score;
                System.out.println("打分成功!");
                return;
            }
        }
        System.out.println("未找到该歌手!");
    }

    public static void queryData() //查询歌手评分信息模块
    {
        System.out.println("1. 查询全部信息");
        System.out.println("2. 按姓名查询");
        int choice = scanner.nextInt();
        scanner.nextLine(); // consume newline

        switch (choice) 
        {
            case 1:
                for (Singer singer : singers) 
                {
                    System.out.print("歌手: " + singer.name + ", 分数为: ");
                    for(int p=0;p<singer._size;p++)
                    {
                        System.out.print(singer.score[p]+"  ");
                    }
                    System.out.println("");
                }
                try
                {
                    Thread.sleep(4000);
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                break;
            case 2:
                System.out.print("请输入歌手姓名: ");
                String name = scanner.nextLine();
                for (Singer singer : singers) {
                    if (singer.name.equals(name)) 
                    {
                        System.out.print("歌手: " + singer.name + ", 分数为: ");
                        for(int p=0;p<singer._size;p++)
                        {
                            System.out.print(singer.score[p]+"  ");
                        }
                        try
                        {
                            Thread.sleep(3000);
                        }
                        catch(InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                        return;
                    }
                }
                System.out.println("未找到该歌手!");
                break;
        }
    }

    public static void sortScores() //歌手排名模块
    {
        for (Singer singer : singers)
        {
            singer.score=singer.Sort();
            for(int p=0;p<singer._size;p++)
            {
                System.out.print(singer.score[p]+" ");
            }
            if(singer._size>2)
            {
                singer.Sum(1,singer._size-1);
                singer._sum/=(singer._size-2);
            }
            else
            {
                singer.Sum(0,singer._size);
                singer._sum/=singer._size;
            }
        }
        singers.sort(Comparator.comparingDouble(singer -> -singer._sum));
        System.out.println("按成绩排序:");
        for (Singer singer : singers) 
        {
            System.out.println("歌手: " + singer.name + ", 分数: " + singer._sum);
        }
        try
        {
            Thread.sleep(4000);
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}